package base

import (
	"gddgame.cc/galaxy/common/dsl/game"
	"gddgame.cc/galaxy/utils/orm"
)

type BaseData struct {
	// base不会修改，保持引用即可
	Bases map[int]*ObjectBaseExt

	baseModel *orm.Model
}

func GetBaseData(env *game.Environment) (*BaseData, error) {
	var err error
	data := &BaseData{}

	if data.baseModel, _, data.Bases, err = getBaseData(env); err != nil {
		return nil, err
	}

	return data, err
}

func getBaseData(env *game.Environment) (*orm.Model, []SettingBase, map[int]*ObjectBaseExt, error) {
	var err error

	model, err := env.GetModel(SettingBaseName)
	if err != nil {
		return nil, nil, nil, err
	}
	var bases []SettingBase
	_, err = model.Query().Find(&bases)
	if err != nil {
		return nil, nil, nil, err
	}
	baseMap := make(map[int]*ObjectBaseExt, len(bases))
	for k := range bases {
		baseMap[bases[k].Id] = NewObjectBase(&bases[k])
	}
	return model, bases, baseMap, nil
}
